#Turnbull-Dugarte & López-Ortega (2025)

#---- Replication script #2 ----
#---- REQUIRES COMPLETION OF SCRIPT #1 FIRST ----

###FIGURE 4###
calculate_conf_intervals <- function(x) {
  x <- as.factor(x)
  if (sum(is.na(x)) < length(x)) {  
    table_x <- table(x)
    if (length(table_x) == 2) {  
      return(binom.test(table_x[2], sum(table_x))$conf.int)
    }
  }
  return(NA)
}

proportionsUK <- colMeans(directUK[c("direct_votesReform", "direct_votesGreen", "direct_votesLab", "direct_votesCon", "direct_putin", "direct_onlyfans")], na.rm = TRUE)


conf_intervalsUK <- sapply(directUK[c("direct_votesReform", "direct_votesGreen", "direct_votesLab", "direct_votesCon", "direct_putin", "direct_onlyfans")], calculate_conf_intervals)

lower_boundUK <- conf_intervalsUK[1, ]
upper_boundUK <- conf_intervalsUK[2, ]

plot_dataUK <- data.frame(Party = c("Supports Reform UK", "Supports Greens", "Supports Labour", "Supports Conservative", "Supports Putin", "Has an OnlyFans profile"),
                          Proportion = proportionsUK,
                          Lower_CI = lower_boundUK,
                          Upper_CI = upper_boundUK)
plot_dataUK$farright <- ifelse(plot_dataUK$Party == "Supports Reform UK", "yes", "no")

plot_dataUK$Party <- reorder(plot_dataUK$Party, plot_dataUK$Proportion, FUN = function(x) -x)

UKobserveplot<- ggplot(plot_dataUK, aes(x = Party, y = Proportion, fill = farright)) +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin = Lower_CI, ymax = Upper_CI), width = 0, color = "black", size = 1) +
  scale_fill_manual(values = c("grey80","grey57")) +
  scale_color_manual(values = c("grey80","grey57")) +
  labs(x = "", y = "", 
       title = "UK")+
  ylim(0,1)+
  geom_label(
    aes(label = sprintf("%0.2f", round(Proportion, digits = 2)), x =Party, y = Proportion),
    nudge_x = .25, size=2.5, fontface = "bold", fill= "white", color="black")+
  coord_flip()+
  theme(legend.position ="none",
        plot.title = element_text(face="bold", hjust=.5),
        axis.text.y = element_text(face="bold"))

proportions <- colMeans(directES[c("direct_votesVOX", "direct_votesSumar", "direct_votesPSOE", "direct_votesPP", "direct_putin", "direct_onlyfans")], na.rm = TRUE)
conf_intervals <- sapply(directES[c("direct_votesVOX", "direct_votesSumar", "direct_votesPSOE", "direct_votesPP", "direct_putin", "direct_onlyfans")], calculate_conf_intervals)

lower_bound <- conf_intervals[1, ]
upper_bound <- conf_intervals[2, ]

plot_data <- data.frame(Party = c("Supports VOX", "Supports Sumar", "Supports PSOE", "Supports PP", "Supports Putin", "Has an OnlyFans profile"),
                        Proportion = proportions,
                        Lower_CI = lower_bound,
                        Upper_CI = upper_bound)
plot_data$farright <- ifelse(plot_data$Party == "Supports VOX", "yes", "no")

plot_data$Party <- reorder(plot_data$Party, plot_data$Proportion, FUN = function(x) -x)

ESobserveplot<- ggplot(plot_data, aes(x = Party, y = Proportion, fill = farright)) +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin = Lower_CI, ymax = Upper_CI), width = 0, color = "black", size = 1) +
  scale_fill_manual(values = c("grey80","grey57")) +
  scale_color_manual(values = c("grey80","grey57")) +
  labs(x = "", y = "", 
       title = "Spain")+
  ylim(0,1)+
  geom_label(
    aes(label = sprintf("%0.2f", round(Proportion, digits = 2)), x =Party, y = Proportion),
    nudge_x = .25, size=2.5, fontface = "bold", fill= "white", color="black")+
  coord_flip()+
  theme(legend.position ="none",
        plot.title = element_text(face="bold", hjust=0.5),
        axis.text.y = element_text(face="bold"))


UKobserveplot+ESobserveplot
ggsave("figures/Figure4.png", dpi=900, height=9, units="cm")


###FIGURE 5###
directES <- directES %>%
  filter(!(partyID_full %in% c("Other", "VOX")))
mod1<- lm(direct_votesVOX ~ partyID_full, data=directES)

mean_votesVOX <- aggregate(direct_votesVOX ~ partyID_full, data = directES, FUN = mean)
se_votesVOX <- aggregate(direct_votesVOX ~ partyID_full, data = directES, FUN = function(x) sd(x)/sqrt(length(x)))
directvotesVOX <- merge(mean_votesVOX, se_votesVOX, by = "partyID_full")
colnames(directvotesVOX) <- c("partyID_full", "mean_votesVOX", "se_votesVOX")

directvotesVOX$lower90 <- directvotesVOX$mean_votesVOX - (1.645 * directvotesVOX$se_votesVOX)
directvotesVOX$higher90 <- directvotesVOX$mean_votesVOX + (1.645 * directvotesVOX$se_votesVOX)
directvotesVOX$lower95 <- directvotesVOX$mean_votesVOX - (1.96 * directvotesVOX$se_votesVOX)
directvotesVOX$higher95 <- directvotesVOX$mean_votesVOX + (1.96 * directvotesVOX$se_votesVOX)
directvotesVOX$lower99 <- directvotesVOX$mean_votesVOX - (2.58 * directvotesVOX$se_votesVOX)
directvotesVOX$higher99 <- directvotesVOX$mean_votesVOX + (2.58 * directvotesVOX$se_votesVOX)

votesVOXplot<- directvotesVOX %>% 
  ggplot(aes(reorder(partyID_full, -mean_votesVOX))) +
  geom_vline(xintercept = 0.5, colour="gray", linetype="dashed")+ ##.46 median acceptance
  geom_segment(aes(x=lower90, xend=higher90, y=partyID_full, yend=partyID_full), size=4, alpha=1)+ 
  geom_segment(aes(x=lower95, xend=higher95, y=partyID_full, yend=partyID_full), size=3, alpha=.8)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=partyID_full, yend=partyID_full), size=2, alpha=.6)+
  geom_point(aes(x=mean_votesVOX, y=partyID_full), fill="white", color="grey67", size=2.5,  pch=21)+
  labs(y=NULL, subtitle='Is voting for VOX a red flag in a partner?', x="")+
  theme_ggdist()+
  xlim(0,1)+
  theme(legend.position = "none") +
  geom_label(
    aes(label = sprintf("%0.2f", round(mean_votesVOX, digits = 2)), y = partyID_full, x = mean_votesVOX),
    nudge_y = -.25, size=2.5, fontface = "bold")+
  geom_label(
    aes(label = partyID_full, x = mean_votesVOX, y = partyID_full),
    nudge_y = .25, size=2.5, fontface = "bold")+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        plot.subtitle = element_text(face="bold", hjust=0))


directUK <- directUK %>%
  filter(!(partyID_full %in% c("Plaid Cymru", "SNP", "Reform UK")))
mod2<- lm(direct_votesReform ~ partyID_full, data=directUK)

mean_votesReform <- aggregate(direct_votesReform ~ partyID_full, data = directUK, FUN = mean)
se_votesReform <- aggregate(direct_votesReform ~ partyID_full, data = directUK, FUN = function(x) sd(x)/sqrt(length(x)))

directvotesReform <- merge(mean_votesReform, se_votesReform, by = "partyID_full")
colnames(directvotesReform) <- c("partyID_full", "mean_votesReform", "se_votesReform")

directvotesReform$lower90 <- directvotesReform$mean_votesReform - (1.645 * directvotesReform$se_votesReform)
directvotesReform$higher90 <- directvotesReform$mean_votesReform + (1.645 * directvotesReform$se_votesReform)
directvotesReform$lower95 <- directvotesReform$mean_votesReform - (1.96 * directvotesReform$se_votesReform)
directvotesReform$higher95 <- directvotesReform$mean_votesReform + (1.96 * directvotesReform$se_votesReform)
directvotesReform$lower99 <- directvotesReform$mean_votesReform - (2.58 * directvotesReform$se_votesReform)
directvotesReform$higher99 <- directvotesReform$mean_votesReform + (2.58 * directvotesReform$se_votesReform)

votesReformplot<- directvotesReform %>% 
  ggplot(aes(partyID_full)) +
  geom_vline(xintercept = 0.5, colour="gray", linetype="dashed")+ ##.46 median acceptance
  geom_segment(aes(x=lower90, xend=higher90, y=partyID_full, yend=partyID_full), size=4, alpha=1)+ 
  geom_segment(aes(x=lower95, xend=higher95, y=partyID_full, yend=partyID_full), size=3, alpha=.8)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=partyID_full, yend=partyID_full), size=2, alpha=.6)+
  geom_point(aes(x=mean_votesReform, y=partyID_full), fill="white", color="grey67", size=2.5,  pch=21)+
  labs(y=NULL, subtitle='Is voting for Reform UK a red flag in a partner?', x="")+
  theme_ggdist()+
  xlim(0,1)+
  theme(legend.position = "none") +
  geom_label(
    aes(label = sprintf("%0.2f", round(mean_votesReform, digits = 2)), y = partyID_full, x = mean_votesReform),
    nudge_y = -.25, size=2.5, fontface = "bold")+
  geom_label(
    aes(label = partyID_full, x = mean_votesReform, y = partyID_full),
    nudge_y = .25, size=2.5, fontface = "bold")+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        plot.subtitle = element_text(face="bold", hjust=0))


votesReformplot+votesVOXplot+ plot_annotation(
  caption = 'Confidence intervals at 99%, 95% & 90%')
ggsave("figures/Figure5.png", dpi=900, height=9, units="cm")


###FIGURE6###
mm.1 <- mm(tinder_conjoint_UK, selected ~ FLAG + BI + TRANS + PROS + CONGRUENCE + ETHNICITY + EMOTION + RELATIONSHIP + DISTANCE + EDUCATION_SLIM + JOB_SLIM + PARTY,
           id = ~ ResponseId, h0 = 0.5)

mm.1$lower90 <- mm.1$estimate - (1.645 * mm.1$std.error)
mm.1$higher90 <- mm.1$estimate + (1.645 * mm.1$std.error)
mm.1$lower95 <- mm.1$estimate - (1.96 * mm.1$std.error)
mm.1$higher95 <- mm.1$estimate + (1.96 * mm.1$std.error)
mm.1$lower99 <- mm.1$estimate - (2.58 * mm.1$std.error)
mm.1$higher99 <- mm.1$estimate + (2.58 * mm.1$std.error)
mm.1$lower999 <- mm.1$estimate - (3.29 * mm.1$std.error)
mm.1$higher999 <- mm.1$estimate + (3.29 * mm.1$std.error)
mm.1<-subset(mm.1, feature %in% c("PARTY")) 

mmplot1<- mm.1 %>% 
  ggplot(aes(level)) +
  geom_vline(xintercept = .46, colour="gray", linetype="dashed")+ ##.46 median acceptance
  geom_segment(aes(x=lower90, xend=higher90, y=level, yend=level), size=4, alpha=1)+ 
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=3, alpha=.8)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=2, alpha=.6)+
  facet_grid(feature~., scales='free', space='free') +  
  geom_point(aes(x=estimate, y=level), fill="white", color="grey67", size=2.5,  pch=21)+
  labs(y=NULL, subtitle='Marginal means (MM)', x="")+
  theme_ggdist()+
  theme(legend.position = "none") +
  xlim(.3,.6)+
  geom_label(
    aes(label = sprintf("%0.2f", round(estimate, digits = 2)), y = level, x = estimate),
    nudge_y = .3, size=2.5, fontface = "bold")+
  geom_label(
    aes(label = level, y = level, x = estimate),
    nudge_y = -.3, size=2.5, fontface = "bold")+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        plot.subtitle = element_text(face="bold", hjust=.5))

amce.1 <- amce(tinder_conjoint_UK, selected ~ FLAG + BI + TRANS + PROS + CONGRUENCE + ETHNICITY + EMOTION + RELATIONSHIP + DISTANCE + EDUCATION_SLIM + JOB_SLIM + PARTY,
               id = ~ ResponseId)

amce.1$lower90 <- amce.1$estimate - (1.645 * amce.1$std.error)
amce.1$higher90 <- amce.1$estimate + (1.645 * amce.1$std.error)
amce.1$lower95 <- amce.1$estimate - (1.96 * amce.1$std.error)
amce.1$higher95 <- amce.1$estimate + (1.96 * amce.1$std.error)
amce.1$lower99 <- amce.1$estimate - (2.58 * amce.1$std.error)
amce.1$higher99 <- amce.1$estimate + (2.58 * amce.1$std.error)
amce.1$lower999 <- amce.1$estimate - (3.29 * amce.1$std.error)
amce.1$higher999 <- amce.1$estimate + (3.29 * amce.1$std.error)

amce.1<-subset(amce.1, feature %in% c("PARTY")) 
amce.1<-subset(amce.1, level %in% c("Reform UK", "Lib Dem", "Labour", "Greens")) 

amceplot1<- amce.1 %>% 
  ggplot(aes(level)) +
  geom_segment(aes(x=lower90, xend=higher90, y=level, yend=level), size=4, alpha=1)+ 
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=3, alpha=.8)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=2, alpha=.6)+
  facet_grid(feature~., scales='free', space='free') +  
  geom_point(aes(x=estimate, y=level), fill="white", color="grey67", size=2.5,  pch=21)+
  labs(y=NULL, subtitle='Average marginal component effect (AMCE)', x="")+
  theme_ggdist()+
  theme(legend.position = "none") +
  xlim(-.2,.22)+
  geom_label(
    aes(label = sprintf("%0.2f", round(estimate, digits = 2)), y = level, x = estimate),
    nudge_y = .25, size=2.5, fontface = "bold")+
  geom_label(
    aes(label = level, y = level, x = estimate),
    nudge_y = -.25, size=2.5, fontface = "bold")+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        plot.subtitle = element_text(face="bold", hjust=.5)) + 
  geom_vline(xintercept = 0, colour="gray", linetype="dashed") ##.46 median acceptance

mmplot1+amceplot1+
  plot_annotation(caption = 'Confidence intervals at 99%, 95% & 90%')
ggsave(filename = here("figures/Figure6.png"),
       height=9, units="cm", dpi = 900)


###FIGURE 7###
mmES.1 <- mm(tinder_conjoint_ES, selected ~ FLAG + BI + TRANS + VEG + 
               CONGRUENCE + RELATIONSHIP + DISTANCE + EDUCATION_SLIM + 
               JOB_SLIM + PARTY, id = ~ ResponseId, h0 = 0.5)

mmES.1$lower90 <- mmES.1$estimate - (1.645 * mmES.1$std.error)
mmES.1$higher90 <- mmES.1$estimate + (1.645 * mmES.1$std.error)
mmES.1$lower95 <- mmES.1$estimate - (1.96 * mmES.1$std.error)
mmES.1$higher95 <- mmES.1$estimate + (1.96 * mmES.1$std.error)
mmES.1$lower99 <- mmES.1$estimate - (2.58 * mmES.1$std.error)
mmES.1$higher99 <- mmES.1$estimate + (2.58 * mmES.1$std.error)
mmES.1$lower999 <- mmES.1$estimate - (3.29 * mmES.1$std.error)
mmES.1$higher999 <- mmES.1$estimate + (3.29 * mmES.1$std.error)

mmES.1<-subset(mmES.1, feature %in% c("PARTY")) 

ESmmplot1<- mmES.1 %>% 
  ggplot(aes(level)) +
  geom_vline(xintercept = .44, colour="gray", linetype="dashed")+ ##.46 median acceptance
  geom_segment(aes(x=lower90, xend=higher90, y=level, yend=level), size=4, alpha=1)+ 
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=3, alpha=.8)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=2, alpha=.6)+
  facet_grid(feature~., scales='free', space='free') +  
  geom_point(aes(x=estimate, y=level), fill="white", color="grey67", size=2.5,  pch=21)+
  labs(y=NULL, subtitle='Marginal means (MM)', x="")+
  theme_ggdist()+
  theme(legend.position = "none") +
  xlim(.3,.6)+
  geom_label(
    aes(label = sprintf("%0.2f", round(estimate, digits = 2)), y = level, x = estimate),
    nudge_y = .25, size=2.5, fontface = "bold")+
  geom_label(
    aes(label = level, y = level, x = estimate),
    nudge_y = -.25, size=2.5, fontface = "bold")+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        plot.subtitle = element_text(face="bold", hjust=.5))

amceES.1 <- amce(tinder_conjoint_ES, selected ~ FLAG + BI + TRANS + VEG +
                   CONGRUENCE + RELATIONSHIP + DISTANCE + EDUCATION_SLIM +
                   JOB_SLIM + PARTY, id = ~ ResponseId)

amceES.1$lower90 <- amceES.1$estimate - (1.645 * amceES.1$std.error)
amceES.1$higher90 <- amceES.1$estimate + (1.645 * amceES.1$std.error)
amceES.1$lower95 <- amceES.1$estimate - (1.96 * amceES.1$std.error)
amceES.1$higher95 <- amceES.1$estimate + (1.96 * amceES.1$std.error)
amceES.1$lower99 <- amceES.1$estimate - (2.58 * amceES.1$std.error)
amceES.1$higher99 <- amceES.1$estimate + (2.58 * amceES.1$std.error)
amceES.1$lower999 <- amceES.1$estimate - (3.29 * amceES.1$std.error)
amceES.1$higher999 <- amceES.1$estimate + (3.29 * amceES.1$std.error)

amceES.1<-subset(amceES.1, feature %in% c("PARTY")) 
amceES.1<-subset(amceES.1, level %in% c("PSOE", "Sumar", "VOX")) 

ESamceplot1<- amceES.1 %>% 
  ggplot(aes(level)) +
  geom_vline(xintercept = 0, colour="gray", linetype="dashed")+ ##.46 median acceptance
  geom_segment(aes(x=lower90, xend=higher90, y=level, yend=level), size=4, alpha=1)+ 
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=3, alpha=.8)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=2, alpha=.6)+
  facet_grid(feature~., scales='free', space='free') +  
  geom_point(aes(x=estimate, y=level), fill="white", color="grey67", size=2.5,  pch=21)+
  labs(y=NULL, subtitle='Average marginal component effect (AMCE)', x="")+
  theme_ggdist()+
  theme(legend.position = "none") +
  xlim(-.2,.2)+
  geom_label(
    aes(label = sprintf("%0.2f", round(estimate, digits = 2)), y = level, x = estimate),
    nudge_y = .2, size=2.5, fontface = "bold")+
  geom_label(
    aes(label = level, y = level, x = estimate),
    nudge_y = -.2, size=2.5, fontface = "bold")+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        plot.subtitle = element_text(face="bold", hjust=.5))

ESmmplot1+ESamceplot1+
  plot_annotation(caption = 'Confidence intervals at 99%, 95% & 90%')
ggsave(filename = here("figures/Figure7.png"),
       height=9, units="cm", dpi = 900)


###FIGURE 8###
byLR.amce<- cj(tinder_conjoint_UK_noReform, selected ~  FLAG + BI + PROS + TRANS + CONGRUENCE + ETHNICITY + EMOTION +RELATIONSHIP + DISTANCE + EDUCATION_SLIM + JOB_SLIM + PARTY,
               id = ~ ResponseId, estimate="amce",by = ~ derecha)

byLR.amce$lower90 <- byLR.amce$estimate - (1.645 * byLR.amce$std.error)
byLR.amce$higher90 <- byLR.amce$estimate + (1.645 * byLR.amce$std.error)
byLR.amce$lower95 <- byLR.amce$estimate - (1.96 * byLR.amce$std.error)
byLR.amce$higher95 <- byLR.amce$estimate + (1.96 * byLR.amce$std.error)
byLR.amce$lower99 <- byLR.amce$estimate - (2.58 * byLR.amce$std.error)
byLR.amce$higher99 <- byLR.amce$estimate + (2.58 * byLR.amce$std.error)

byLR.amce<-subset(byLR.amce, feature %in% c("PARTY")) 

amceplot2 <- ggplot(byLR.amce, aes(y = level, x = estimate, color = BY)) +
  scale_shape_manual(values = c(10, 18)) +
  scale_fill_manual(values = col2) +
  scale_color_manual(values = col2) +
  facet_grid(feature~., scales='free', space='free') +  
  geom_vline(xintercept = 0, colour="grey", linetype="dashed")+
  geom_segment(data = subset(byLR.amce, BY == "Right-wing"),
               aes(x = lower90, xend = higher90, y = level, yend = level),
               size = 4, alpha = 1,  position = position_nudge(y = .2)) + 
  geom_segment(data = subset(byLR.amce, BY == "Right-wing"),
               aes(x = lower95, xend = higher95, y = level, yend = level),
               size = 3, alpha = .8,  position = position_nudge(y = .2)) + 
  geom_segment(data = subset(byLR.amce, BY == "Right-wing"),
               aes(x = lower99, xend = higher99, y = level, yend = level),
               size = 2, alpha = .6, position = position_nudge(y = .2)) + 
  geom_point(data = subset(byLR.amce, BY == "Right-wing"),
             aes(x = estimate, y = level), fill = "white", color = "grey67", size = 2.5, pch = 21,  
             position = position_nudge(y = .2))+
  geom_segment(data = subset(byLR.amce, BY == "Left-wing"),
               aes(x = lower90, xend = higher90, y = level, yend = level),
               size = 4, alpha = 1) + 
  geom_segment(data = subset(byLR.amce, BY == "Left-wing"),
               aes(x = lower95, xend = higher95, y = level, yend = level),
               size = 3, alpha = .8) + 
  geom_segment(data = subset(byLR.amce, BY == "Left-wing"),
               aes(x = lower99, xend = higher99, y = level, yend = level),
               size = 2, alpha = .6) + 
  geom_point(data = subset(byLR.amce, BY == "Left-wing"),
             aes(x = estimate, y = level), fill = "white", color = "grey67", size = 2.5, pch = 21) +
  geom_label(data = subset(byLR.amce, BY == "Right-wing"),
             aes(label = sprintf("%0.2f", round(estimate, digits = 2)), y = level, x = estimate),
             position = position_nudge(y = .475), size = 2.5, fontface = "bold"
  ) +
  geom_label(data = subset(byLR.amce, BY == "Left-wing"),
             aes(label = sprintf("%0.2f", round(estimate, digits = 2)), y = level, x = estimate),
             position = position_nudge(y = -.275), size = 2.5, fontface = "bold"
  ) +
  annotate("label", x = .16, y = 1.2, label = "Centre-right partisan",
           color="grey50", size=2.5, fontface="bold", hjust=.5)+
  annotate("label", x = .16, y = .8, label = "All left-wing partisans",
           color="black", size=2.5, fontface="bold", hjust=.5)+
  labs(y = NULL, subtitle = 'Conditional AMCE', x="")+
  theme_ggdist() +
  xlim(-.2,.3)+
  theme(legend.position = "none",
        strip.background = element_blank(),
        strip.text = element_blank(),
        plot.subtitle = element_text(face="bold", hjust=.5)) 

byLR.mm<- cj(tinder_conjoint_UK_noReform, selected ~ FLAG + BI + PROS + TRANS + CONGRUENCE + ETHNICITY + EMOTION +RELATIONSHIP + DISTANCE + EDUCATION_SLIM + JOB_SLIM + PARTY,
             id = ~ ResponseId, estimate="mm",by = ~ derecha)

byLR.mm$lower90 <- byLR.mm$estimate - (1.645 * byLR.mm$std.error)
byLR.mm$higher90 <- byLR.mm$estimate + (1.645 * byLR.mm$std.error)
byLR.mm$lower95 <- byLR.mm$estimate - (1.96 * byLR.mm$std.error)
byLR.mm$higher95 <- byLR.mm$estimate + (1.96 * byLR.mm$std.error)
byLR.mm$lower99 <- byLR.mm$estimate - (2.58 * byLR.mm$std.error)
byLR.mm$higher99 <- byLR.mm$estimate + (2.58 * byLR.mm$std.error)

byLR.mm<-subset(byLR.mm, feature %in% c("PARTY")) 

mmplot2 <- ggplot(byLR.mm, aes(y = level, x = estimate, color = BY)) +
  scale_shape_manual(values = c(10, 18)) +
  scale_fill_manual(values = col2) +
  scale_color_manual(values = col2) +
  geom_segment(data = subset(byLR.mm, BY == "Right-wing"),
               aes(x = lower90, xend = higher90, y = level, yend = level),
               size = 4, alpha = 1,  position = position_nudge(y = .2)) + 
  geom_segment(data = subset(byLR.mm, BY == "Right-wing"),
               aes(x = lower95, xend = higher95, y = level, yend = level),
               size = 3, alpha = .8,  position = position_nudge(y = .2)) + 
  geom_segment(data = subset(byLR.mm, BY == "Right-wing"),
               aes(x = lower99, xend = higher99, y = level, yend = level),
               size = 2, alpha = .6,  position = position_nudge(y = .2)) +
  geom_point(data = subset(byLR.mm, BY == "Right-wing"),
             aes(x = estimate, y = level), fill = "white", color = "grey67", size = 2.5, pch = 21,  
             position = position_nudge(y = .2))+
  geom_segment(data = subset(byLR.mm, BY == "Left-wing"),
               aes(x = lower90, xend = higher90, y = level, yend = level),
               size = 4, alpha = 1) + 
  geom_segment(data = subset(byLR.mm, BY == "Left-wing"),
               aes(x = lower95, xend = higher95, y = level, yend = level),
               size = 3, alpha = .8) + 
  geom_segment(data = subset(byLR.mm, BY == "Left-wing"),
               aes(x = lower99, xend = higher99, y = level, yend = level),
               size = 2, alpha = .6) +
  geom_point(data = subset(byLR.mm, BY == "Left-wing"),
             aes(x = estimate, y = level), fill = "white", color = "grey67", size = 2.5, pch = 21) +
  geom_label(data = subset(byLR.mm, BY == "Right-wing"),
             aes(label = sprintf("%0.2f", round(estimate, digits = 2)), y = level, x = estimate),
             position = position_nudge(y = .475), size = 2.5, fontface = "bold"
  ) +
  geom_label(data = subset(byLR.mm, BY == "Left-wing"),
             aes(label = sprintf("%0.2f", round(estimate, digits = 2)), y = level, x = estimate),
             position = position_nudge(y = -.275), size = 2.5, fontface = "bold"
  ) +
  labs(y = NULL, subtitle = 'Conditional MM',
       x="") +
  theme_ggdist() +
  theme(legend.position = "none", 
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        plot.subtitle = element_text(face="bold", hjust=.5)) + 
  xlim(.2, .7)

byLR.diff<- mm_diffs(tinder_conjoint_UK_noReform, selected ~ FLAG + BI + PROS + TRANS + CONGRUENCE + ETHNICITY + EMOTION +RELATIONSHIP + DISTANCE + EDUCATION_SLIM + JOB_SLIM + PARTY,
                     id = ~ ResponseId, by = ~ derecha)

byLR.diff$lower90 <- byLR.diff$estimate - (1.645 * byLR.diff$std.error)
byLR.diff$higher90 <- byLR.diff$estimate + (1.645 * byLR.diff$std.error)
byLR.diff$lower95 <- byLR.diff$estimate - (1.96 * byLR.diff$std.error)
byLR.diff$higher95 <- byLR.diff$estimate + (1.96 * byLR.diff$std.error)
byLR.diff$lower99 <- byLR.diff$estimate - (2.58 * byLR.diff$std.error)
byLR.diff$higher99 <- byLR.diff$estimate + (2.58 * byLR.diff$std.error)

byLR.diff<-subset(byLR.diff, feature %in% c("PARTY")) 

LRdiffplot<- byLR.diff%>% 
  ggplot(aes(level)) +
  geom_vline(xintercept = 0, colour="gray", linetype="dashed")+ ##.46 median acceptance
  geom_segment(aes(x=lower90, xend=higher90, y=level, yend=level), size=4, alpha=1)+ 
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=3, alpha=.8)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=2, alpha=.6)+
  facet_grid(feature~., scales='free', space='free') +  
  geom_point(aes(x=estimate, y=level), fill="white", color="grey67", size=2.5,  pch=21)+
  labs(y=NULL, subtitle='Pairwise difference in MM',
       x="")+
  theme_ggdist()+
  theme(legend.position = "none") +
  geom_label(
    aes(label = sprintf("%0.2f", round(estimate, digits = 2)), y = level, x = estimate),
    nudge_y = .3, size=2.5, fontface = "bold")+
  geom_label(
    aes(label = level, y = level, x = estimate),
    nudge_y = -.3, size=2.5, fontface = "bold")+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        plot.subtitle = element_text(face="bold", hjust=.5))+
  xlim(-.2, .27)

amceplot2+mmplot2+LRdiffplot+
  plot_annotation(caption = 'Confidence intervals at 99%, 95% & 90%')
ggsave(filename = here("figures/Figure8.png"), 
       height=9.5, units="cm", dpi = 900)

###FIGURE 9###
byLR.amce<- cj(tinder_conjoint_ES_noVOX, selected ~ FLAG + BI + VEG + TRANS + CONGRUENCE + ETHNICITY + EMOTION +RELATIONSHIP + DISTANCE + EDUCATION_SLIM + JOB_SLIM + PARTY,
               id = ~ ResponseId, estimate="amce",by = ~ derecha)

byLR.amce$lower90 <- byLR.amce$estimate - (1.645 * byLR.amce$std.error)
byLR.amce$higher90 <- byLR.amce$estimate + (1.645 * byLR.amce$std.error)
byLR.amce$lower95 <- byLR.amce$estimate - (1.96 * byLR.amce$std.error)
byLR.amce$higher95 <- byLR.amce$estimate + (1.96 * byLR.amce$std.error)
byLR.amce$lower99 <- byLR.amce$estimate - (2.58 * byLR.amce$std.error)
byLR.amce$higher99 <- byLR.amce$estimate + (2.58 * byLR.amce$std.error)

byLR.amce<-subset(byLR.amce, feature %in% c("PARTY")) 

ESamceplot2 <- ggplot(byLR.amce, aes(y = level, x = estimate, color = BY)) +
  scale_shape_manual(values = c(10, 18)) +
  scale_fill_manual(values = col2) +
  scale_color_manual(values = col2) +
  facet_grid(feature~., scales='free', space='free') +  
  geom_vline(xintercept = 0, colour="grey", linetype="dashed")+
  geom_segment(data = subset(byLR.amce, BY == "Right-wing"),
               aes(x = lower90, xend = higher90, y = level, yend = level),
               size = 4, alpha = 1,  position = position_nudge(y = .2)) + 
  geom_segment(data = subset(byLR.amce, BY == "Right-wing"),
               aes(x = lower95, xend = higher95, y = level, yend = level),
               size = 3, alpha = .8,  position = position_nudge(y = .2)) + 
  geom_segment(data = subset(byLR.amce, BY == "Right-wing"),
               aes(x = lower99, xend = higher99, y = level, yend = level),
               size = 2, alpha = .6, position = position_nudge(y = .2)) + 
  geom_point(data = subset(byLR.amce, BY == "Right-wing"),
             aes(x = estimate, y = level), fill = "white", color = "grey67", size = 2.5, pch = 21,  
             position = position_nudge(y = .2))+
  geom_segment(data = subset(byLR.amce, BY == "Left-wing"),
               aes(x = lower90, xend = higher90, y = level, yend = level),
               size = 4, alpha = 1) + 
  geom_segment(data = subset(byLR.amce, BY == "Left-wing"),
               aes(x = lower95, xend = higher95, y = level, yend = level),
               size = 3, alpha = .8) + 
  geom_segment(data = subset(byLR.amce, BY == "Left-wing"),
               aes(x = lower99, xend = higher99, y = level, yend = level),
               size = 2, alpha = .6) + 
  geom_point(data = subset(byLR.amce, BY == "Left-wing"),
             aes(x = estimate, y = level), fill = "white", color = "grey67", size = 2.5, pch = 21) +
  geom_label(data = subset(byLR.amce, BY == "Right-wing"),
             aes(label = sprintf("%0.2f", round(estimate, digits = 2)), y = level, x = estimate),
             position = position_nudge(y = .45), size = 2.5, fontface = "bold"
  ) +
  geom_label(data = subset(byLR.amce, BY == "Left-wing"),
             aes(label = sprintf("%0.2f", round(estimate, digits = 2)), y = level, x = estimate),
             position = position_nudge(y = -.25), size = 2.5, fontface = "bold"
  ) +
  annotate("label", x = .16, y = 1.2, label = "Centre-right partisan",
           color="grey50", size=3, fontface="bold", hjust=.5)+
  annotate("label", x = .16, y = .8, label = "All left-wing partisans",
           color="black", size=3, fontface="bold", hjust=.5)+
  labs(y = NULL, subtitle = 'Conditional AMCE', x="")+
  theme_ggdist() +
  xlim(-.25,.3)+
  theme(legend.position = "none",
        strip.background = element_blank(),
        strip.text = element_blank(),
        plot.subtitle = element_text(face="bold", hjust=.5)) 

byLR.ESmm<- cj(tinder_conjoint_ES_noVOX, selected ~ FLAG + BI + VEG + TRANS + CONGRUENCE + RELATIONSHIP + DISTANCE + EDUCATION_SLIM + JOB_SLIM + PARTY,
               id = ~ ResponseId, estimate="mm", by = ~ derecha)

byLR.ESmm$lower90 <- byLR.ESmm$estimate - (1.645 * byLR.ESmm$std.error)
byLR.ESmm$higher90 <- byLR.ESmm$estimate + (1.645 * byLR.ESmm$std.error)
byLR.ESmm$lower95 <- byLR.ESmm$estimate - (1.96 * byLR.ESmm$std.error)
byLR.ESmm$higher95 <- byLR.ESmm$estimate + (1.96 * byLR.ESmm$std.error)
byLR.ESmm$lower99 <- byLR.ESmm$estimate - (2.58 * byLR.ESmm$std.error)
byLR.ESmm$higher99 <- byLR.ESmm$estimate + (2.58 * byLR.ESmm$std.error)

byLR.ESmm<-subset(byLR.ESmm, feature %in% c("PARTY")) 

ESmmplot2 <- ggplot(byLR.ESmm, aes(y = level, x = estimate, color = BY)) +
  geom_vline(xintercept = .44, colour="gray", linetype="dashed")+ ##.46 median acceptance
  scale_shape_manual(values = c(10, 18)) +
  scale_fill_manual(values = col2) +
  scale_color_manual(values = col2) +
  geom_segment(data = subset(byLR.ESmm, BY == "Right-wing"),
               aes(x = lower90, xend = higher90, y = level, yend = level),
               size = 4, alpha = 1,  position = position_nudge(y = .2)) + 
  geom_segment(data = subset(byLR.ESmm, BY == "Right-wing"),
               aes(x = lower95, xend = higher95, y = level, yend = level),
               size = 3, alpha = .8,  position = position_nudge(y = .2)) + 
  geom_segment(data = subset(byLR.ESmm, BY == "Right-wing"),
               aes(x = lower99, xend = higher99, y = level, yend = level),
               size = 2, alpha = .6,  position = position_nudge(y = .2)) +
  geom_point(data = subset(byLR.ESmm, BY == "Right-wing"),
             aes(x = estimate, y = level), fill = "white", color = "grey67", size = 2.5, pch = 21,  
             position = position_nudge(y = .2))+
  geom_segment(data = subset(byLR.ESmm, BY == "Left-wing"),
               aes(x = lower90, xend = higher90, y = level, yend = level),
               size = 4, alpha = 1) + 
  geom_segment(data = subset(byLR.ESmm, BY == "Left-wing"),
               aes(x = lower95, xend = higher95, y = level, yend = level),
               size = 3, alpha = .8) + 
  geom_segment(data = subset(byLR.ESmm, BY == "Left-wing"),
               aes(x = lower99, xend = higher99, y = level, yend = level),
               size = 2, alpha = .6) +
  geom_point(data = subset(byLR.ESmm, BY == "Left-wing"),
             aes(x = estimate, y = level), fill = "white", color = "grey67", size = 2.5, pch = 21) +
  geom_label(data = subset(byLR.ESmm, BY == "Right-wing"),
             aes(label = sprintf("%0.2f", round(estimate, digits = 2)), y = level, x = estimate),
             position = position_nudge(y = .45), size = 2.5, fontface = "bold"
  ) +
  geom_label(data = subset(byLR.ESmm, BY == "Left-wing"),
             aes(label = sprintf("%0.2f", round(estimate, digits = 2)), y = level, x = estimate),
             position = position_nudge(y = -.25), size = 2.5, fontface = "bold"
  ) +
  labs(y = NULL, subtitle = 'Conditional MM',
       x="") +
  theme_ggdist() +
  theme(legend.position = "none",
        strip.background = element_blank(),
        strip.text = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        plot.subtitle = element_text(face="bold", hjust=.5)) + 
  xlim(.2, .7)


byLR.ESdiff<- mm_diffs(tinder_conjoint_ES_noVOX, selected ~ FLAG + BI + VEG + TRANS + CONGRUENCE + RELATIONSHIP + DISTANCE + EDUCATION_SLIM + JOB_SLIM + PARTY,
                       id = ~ ResponseId, by = ~ derecha)

byLR.ESdiff$lower90 <- byLR.ESdiff$estimate - (1.645 * byLR.ESdiff$std.error)
byLR.ESdiff$higher90 <- byLR.ESdiff$estimate + (1.645 * byLR.ESdiff$std.error)
byLR.ESdiff$lower95 <- byLR.ESdiff$estimate - (1.96 * byLR.ESdiff$std.error)
byLR.ESdiff$higher95 <- byLR.ESdiff$estimate + (1.96 * byLR.ESdiff$std.error)
byLR.ESdiff$lower99 <- byLR.ESdiff$estimate - (2.58 * byLR.ESdiff$std.error)
byLR.ESdiff$higher99 <- byLR.ESdiff$estimate + (2.58 * byLR.ESdiff$std.error)

byLR.ESdiff<-subset(byLR.ESdiff, feature %in% c("PARTY")) 

LRdiffplotES<- byLR.ESdiff%>% 
  ggplot(aes(level)) +
  geom_vline(xintercept = 0, colour="gray", linetype="dashed")+ ##.46 median acceptance
  geom_segment(aes(x=lower90, xend=higher90, y=level, yend=level), size=4, alpha=1)+ 
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=3, alpha=.8)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=2, alpha=.6)+
  facet_grid(feature~., scales='free', space='free') +  
  geom_point(aes(x=estimate, y=level), fill="white", color="grey67", size=2.5,  pch=21)+
  labs(y=NULL, subtitle='Pairwise difference in MM', x="")+
  theme_ggdist()+
  theme(legend.position = "none") +
  geom_label(
    aes(label = sprintf("%0.2f", round(estimate, digits = 2)), y = level, x = estimate),
    nudge_y = .25, size=2.5, fontface = "bold")+
  geom_label(
    aes(label = level, y = level, x = estimate),
    nudge_y = -.25, size=2.5, fontface = "bold")+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        plot.subtitle = element_text(face="bold", hjust=.5))+
  xlim(-.2, .27)

ESamceplot2+ESmmplot2+LRdiffplotES+
  plot_annotation(caption = 'Confidence intervals at 99%, 95% & 90%')
ggsave(filename = here("figures/Figure9.png"),
       height=9, units="cm", dpi = 900)



###FIGURE 10###
library(readstata13)
BESdata <- read.dta13("data/BES_W25.dta")
BESdata$Brexit_n <-as.numeric(BESdata$likeBrexitParty)
BESdata$Lab_n <- as.numeric(BESdata$likeLab)
BESdata_Rescaled <- BESdata

rescale <- function(x) {
  (x - min(x, na.rm = TRUE)) / (max(x, na.rm = TRUE) - min(x, na.rm = TRUE)) 
} 
vars_to_rescale <- c("Brexit_n", "Lab_n") 

for (var in vars_to_rescale) { 
  BESdata_Rescaled [[var]] <- rescale(BESdata_Rescaled [[var]]) 
} 

BESdata_Rescaled$DisLikeLab_n <- 1 - (BESdata_Rescaled$Lab_n)
BESdata_Rescaled$RelativeLike <- BESdata_Rescaled$Brexit_n - BESdata_Rescaled$Lab_n
BESdata_Rescaled$RelativeDisLike <- BESdata_Rescaled$DisLikeLab_n - BESdata_Rescaled$Brexit_n

vars_to_rescale <- c("DisLikeLab_n") 

for (var in vars_to_rescale) { 
  BESdata_Rescaled [[var]] <- rescale(BESdata_Rescaled [[var]]) 
} 

BESdata_Rescaled <- BESdata_Rescaled %>%
  mutate(Party = ifelse(!is.na(partyIdSqueeze), partyIdSqueeze, partyId)) #Tory == 1
BESdata_Rescaled_Tory <- subset(BESdata_Rescaled, Party==1) 

meanBrex <- mean(BESdata_Rescaled_Tory$Brexit_n, na.rm = TRUE)
meanLab <- mean(BESdata_Rescaled_Tory$DisLikeLab_n, na.rm = TRUE)

UK_Like <- ggplot(data = BESdata_Rescaled_Tory) +
  geom_vline(xintercept = meanBrex,color = "grey25") +
  geom_vline(xintercept = meanLab, color = "grey70") +
  geom_density(aes(x = DisLikeLab_n, fill = "Labour"), alpha = 0.75, color = "black", linetype = "dashed") +
  geom_density(aes(x = Brexit_n, fill = "Reform/Brexit"), alpha = 0.75, color = "grey25") +
  ylim(0, 5) +
  xlim(0, 1) +
  scale_fill_manual(values = c("grey70", "grey25"), labels = c("Reform/Brexit", "Labour")) +
  scale_color_manual(values = c("black", "black")) +
  labs(x = "Affect towards in- & out-bloc partisans", 
       title = "UK",subtitle = "") +
  theme_ggdist() +  # Include the ggdist theme
  theme(legend.position = "none",
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank(),
        axis.title.x = element_text(size = 8)) +
  annotate("text", x = 0, y = 3.55, label = "In-bloc positive affect",
           color = "grey25", size = 3, fontface = "bold", hjust = 0) +
  annotate("text", x = 0, y = 3.95, label = "Out-bloc negative affect",
           color = "grey70", size = 3, fontface = "bold", hjust = 0) +
  annotate("label", x = meanLab + 0.025, y = 4, size = 3,
           label = round(meanLab, 2), color = "grey70", vjust = -0.5) +
  annotate("label", x = meanBrex - 0.025, y = 4, size = 3,
           label = round(meanBrex, 2), color = "grey25", vjust = -0.5)
Mean_Dislike <- mean(BESdata_Rescaled_Tory$RelativeDisLike, na.rm = TRUE)

UK_RelDislike <- ggplot(data = BESdata_Rescaled_Tory) +
  geom_vline(xintercept = Mean_Dislike,  color = "grey25") +
  geom_density(aes(x = RelativeDisLike, fill = "Difference"), alpha = 0.75, color = "black") +
  ylim(0, 2) +
  scale_fill_manual(values = c("grey25"), labels = c("Difference")) +
  scale_color_manual(values = c("black", "black")) +
  labs(x = "Relative affect", subtitle = "") +
  theme_ggdist() +  # Include the ggdist theme
  theme(legend.position = "none",
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank(),
        axis.title.x = element_text(size = 8)) +
  annotate("label", x = Mean_Dislike + 0.05, y = 1., size=3,
           label = round(Mean_Dislike, 3), color = "grey25", vjust = -0.5)


Spaindata<- read.dta13("data/spain_survey2.dta")
Spaindata$vox_n <-as.numeric(Spaindata$affect_vox)
Spaindata$psoe_n <- as.numeric(Spaindata$affect_psoe)
Spaindata_Rescaled <- Spaindata

vars_to_rescale <- c("vox_n", "psoe_n") 

for (var in vars_to_rescale) { 
  Spaindata_Rescaled [[var]] <- rescale(Spaindata_Rescaled [[var]]) 
} 

Spaindata_Rescaled$DisLikepsoe_n <- 1 - (Spaindata_Rescaled$psoe_n)
Spaindata_Rescaled$RelativeLike <- Spaindata_Rescaled$vox_n - Spaindata_Rescaled$psoe_n
Spaindata_Rescaled$RelativeDisLike <- Spaindata_Rescaled$DisLikepsoe_n - Spaindata_Rescaled$vox_n

vars_to_rescale <- c("DisLikepsoe_n") 

for (var in vars_to_rescale) { 
  Spaindata_Rescaled [[var]] <- rescale(Spaindata_Rescaled [[var]]) 
} 

Spaindata_Rescaled_PP <- subset(Spaindata_Rescaled, respondent_centreright==1) 

meanVox <- mean(Spaindata_Rescaled_PP$vox_n, na.rm = TRUE)
meanPSOE <- mean(Spaindata_Rescaled_PP$DisLikepsoe_n, na.rm = TRUE)

table(Spaindata_Rescaled$vox_n)
ES_Like <- ggplot(data = Spaindata_Rescaled_PP) +
  geom_vline(xintercept = meanVox,color = "grey25") +
  geom_vline(xintercept = meanPSOE, color = "grey70") +
  geom_density(aes(x = DisLikepsoe_n, fill = "PSOE"), alpha = 0.75, color = "black", linetype = "dashed") +
  geom_density(aes(x = vox_n, fill = "VOX"), alpha = 0.75, color = "grey25") +
  ylim(0, 5) +
  xlim(0, 1) +
  scale_fill_manual(values = c("grey70", "grey25"), labels = c("Labour", "PSOE")) +
  scale_color_manual(values = c("black", "black")) +
  labs(x = "Affect towards in- & out-bloc partisans", 
       title = "Spain", subtitle = "") +
  theme_ggdist() +  
  theme(legend.position = "none",
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank(),
        axis.title.x = element_text(size = 8)) +
  annotate("label", x = meanPSOE + 0.025, y = 3, size = 3,
           label = round(meanPSOE, 2), color = "grey70", vjust = -0.5) +
  annotate("label", x = meanVox - 0.025, y = 3, size = 3,
           label = round(meanVox, 2), color = "grey25", vjust = -0.5)
Mean_Dislike_ES <- mean(Spaindata_Rescaled_PP$RelativeDisLike, na.rm = TRUE)

ES_RelDislike <- ggplot(data = Spaindata_Rescaled_PP) +
  geom_vline(xintercept = Mean_Dislike_ES,  color = "grey25") +
  geom_density(aes(x = RelativeDisLike, fill = "Difference"), alpha = 0.75, color = "black") +
  ylim(0, 2) +
  scale_fill_manual(values = c("grey25"), labels = c("Difference")) +
  scale_color_manual(values = c("black", "black")) +
  labs(x = "Relative affect", subtitle = "") +
  theme_ggdist() +  
  theme(legend.position = "none",
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank(),
        axis.title.x = element_text(size = 8)) +
  annotate("label", x = Mean_Dislike_ES + 0.05, y = 1., size=3,
           label = round(Mean_Dislike_ES, 3), color = "grey25", vjust = -0.5)


combined_affect <- UK_Like+ES_Like+UK_RelDislike+ES_RelDislike+
  plot_layout(nrow = 2, ncol =2, heights =c(2, 1))+
  plot_annotation(title = "Centre-right voters' in-bloc & out-bloc affect",
                  subtitle = "Absolute (upper panel) and relative (lower panel) affect towards in-bloc & out-bloc partians",
                  caption ="Relative affect indicates within-individual difference in negative out-bloc affect & positive in-bloc affect\nUK data from British Election Study Wave 25 (May 2023). Spain data from original survey (June 2024)") &
  theme(plot.title = element_text(face="bold"),
        plot.caption = element_text(size=7))
ggsave("figures/Figure10.png", dpi=600, height=5)




##TABLE 1 and TABLE 2###

vars_to_rescale2 <- c("socialDist_vox", "socialDist_pp", "socialDist_psoe", "socialDist_salf")
for (var in vars_to_rescale2) {
  Spaindata[[var]] <- rescale(Spaindata[[var]])
}
Spaindata$relative_distance <- Spaindata$socialDist_vox - Spaindata$socialDist_psoe
Spaindata$relative_distance2 <- Spaindata$socialDist_salf - Spaindata$socialDist_psoe


BESdata_w19<- read.dta13("data/BES_W19.dta")
BESdata_w19 <- BESdata_w19 %>%
  mutate(
    socialDistCon = ifelse(socialDistCon == "Don't know", NA, socialDistCon),
    socialDistLab = ifelse(socialDistLab == "Don't know", NA, socialDistLab),
    socialDistBrexit = ifelse(socialDistBrexit == "Don't know", NA, socialDistBrexit)
  ) %>%
  rename(socialDistReform = socialDistBrexit) %>%
  mutate(
    respondent_centreright = ifelse(
      partyId == "Conservative" | partyIdSqueeze == "Conservative", 1, 0
    )
  ) %>%
  select(id, respondent_centreright, partyId, socialDistCon, socialDistLab, socialDistReform, wt)

BESdata_w19 <-BESdata_w19%>% 
  mutate(socialDistReform= as.numeric(socialDistReform),
         socialDistCon= as.numeric(socialDistCon),
         socialDistLab= as.numeric(socialDistLab))


vars_to_rescale1 <- c("socialDistReform", "socialDistLab", "socialDistCon")
for (var in vars_to_rescale1) {
  BESdata_w19[[var]] <- rescale(BESdata_w19[[var]])
}
BESdata_w19$relative_distance <- BESdata_w19$socialDistReform - BESdata_w19$socialDistLab


create_dichotomous <- function(x) {
  ifelse(!is.na(x) & x >= 0.5, "Acceptable", ifelse(!is.na(x) & x < 0.5, "Unacceptable", NA))
}
Spaindata$socialDist_vox_dichotomous <- create_dichotomous(Spaindata$socialDist_vox)
Spaindata$socialDist_pp_dichotomous <- create_dichotomous(Spaindata$socialDist_pp)
Spaindata$socialDist_psoe_dichotomous <- create_dichotomous(Spaindata$socialDist_psoe)
Spaindata$socialDist_salf_dichotomous <- create_dichotomous(Spaindata$socialDist_salf)
BESdata_w19$socialDistReform_dichotomous <- create_dichotomous(BESdata_w19$socialDistReform)
BESdata_w19$socialDistCon_dichotomous <- create_dichotomous(BESdata_w19$socialDistCon)
BESdata_w19$socialDistLab_dichotomous <- create_dichotomous(BESdata_w19$socialDistLab)

create_dichotomous2 <- function(x) {
  ifelse(!is.na(x), 
         ifelse(x < 0, "Less acceptable", 
                ifelse(x == 0, "Equally acceptable", "More acceptable")), 
         NA)}

Spaindata$relative_dichotomous <- create_dichotomous2(Spaindata$relative_distance)
Spaindata$relative_dichotomous2 <- create_dichotomous2(Spaindata$relative_distance2)
BESdata_w19$relative_dichotomous <- create_dichotomous2(BESdata_w19$relative_distance)
table(Spaindata$relative_dichotomous)

uk_cright <- subset(BESdata_w19, respondent_centreright == 1)
BESdata_w19 <-BESdata_w19%>% 
  mutate(respondent_centreright= as.factor(respondent_centreright))

spain_cright <- subset(Spaindata, respondent_centreright == 1)
Spaindata <-Spaindata%>% 
  mutate(respondent_centreright= as.factor(respondent_centreright))


calculate_proportions <- function(variable) {
  prop_table <- prop.table(table(variable, useNA = "no"))
  return(prop_table)
}

prop_reform <- calculate_proportions(BESdata_w19$socialDistReform_dichotomous)
prop_con <- calculate_proportions(BESdata_w19$socialDistCon_dichotomous)
prop_lab <- calculate_proportions(BESdata_w19$socialDistLab_dichotomous)

prop_vox <- calculate_proportions(Spaindata$socialDist_vox_dichotomous)
prop_pp <- calculate_proportions(Spaindata$socialDist_pp_dichotomous)
prop_psoe <- calculate_proportions(Spaindata$socialDist_psoe_dichotomous)
prop_salf <- calculate_proportions(Spaindata$socialDist_salf_dichotomous)

relative_spain <- calculate_proportions(spain_cright$relative_dichotomous)
relative_spain2 <- calculate_proportions(spain_cright$relative_dichotomous2)
relative_uk <- calculate_proportions(uk_cright$relative_dichotomous)

proportions_df <- data.frame(
  Value = c("Acceptable", "Unacceptable"),
  socialDistReform = c(prop_reform[1], prop_reform[2]),
  socialDistCon = c(prop_con[1], prop_con[2]),
  socialDistLab = c(prop_lab[1], prop_lab[2]),
  socialDist_vox= c(prop_vox[1], prop_vox[2]),
  socialDist_salf= c(prop_salf[1], prop_salf[2]),
  socialDist_pp = c(prop_pp[1], prop_pp[2]),
  socialDist_psoe = c(prop_psoe[1], prop_psoe[2])
)

proportions_df2 <- data.frame(
  Value = c("Centre-left more acceptable", "Both equally acceptable", "Far-right more acceptable"),
  relative_uk = c(relative_uk[2], relative_uk[1], relative_uk[3]),
  relative_spain = c(relative_spain[2], relative_spain[1], relative_spain[3]),
  relative_spain2 = c(relative_spain2[2], relative_spain2[1], relative_spain2[3]))

proportions_xtable <- xtable(proportions_df, caption = "Proportions of Responses")
proportions_xtable2 <- xtable(proportions_df2, caption = "Proportions of Responses")

print(proportions_xtable, file = "tables/Table1.tex", type = "latex", include.rownames = FALSE)
print(proportions_xtable2, file = "tables/Table2.tex", type = "latex", include.rownames = FALSE)


